package com.mall.service.mall;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mall.client.mall.MallService;
import com.mall.custom.dao.read.MallReadMapper;
import com.mall.custom.dao.write.MallWriteMapper;
import com.mall.pojo.Mall;
import com.mall.pojo.MallExample;
import com.mall.pojo.MallExample.Criteria;
import com.mall.query.Page;
import com.mall.query.search.MallSearchQuery;

@Service
public class MallServiceImpl implements MallService {

	@Autowired
	private MallReadMapper mallReadMapper;
	@Autowired
	private MallWriteMapper mallWriteMapper;

	@Override
	public Mall getMallById(Long mallId) {
		if(mallId == null || mallId <= 0){
			return null;
		}
		return this.mallReadMapper.selectByPrimaryKey(mallId);
	}

	/**
	 * 店铺的ids
	 * 
	 * @param mallIds
	 * @return
	 */
	@Override
	public List<Mall> getMallListByMallIds(List<Long> mallIds) {
		MallExample example = new MallExample();
		Criteria criteria = example.createCriteria();
		criteria.andIdIn(mallIds);
		return mallReadMapper.selectByExample(example);
	}

	@Override
	public List<Mall> getMallListByQuery(MallSearchQuery mallSearchQuery, Page page) {
		if (page == null) {
			page = Page.getInitPage();
		}
		MallExample example = new MallExample();
		example.setLimit(page.getLength());
		example.setStart(page.getBegin());
		if(StringUtils.isNotBlank(mallSearchQuery.getOrderBy())){
			example.setOrderByClause(mallSearchQuery.getOrderBy());
		}else{
			example.setOrderByClause("id desc");
		}
		
		Criteria criteria = example.createCriteria();
		if (mallSearchQuery.getCommKeyId() != null && mallSearchQuery.getCommKeyId() > 0) {
			criteria.andCommOnlyKeyEqualTo(mallSearchQuery.getCommKeyId());
		}

		if (mallSearchQuery.getBeginPrice() != null && mallSearchQuery.getBeginPrice() > 0
				&& mallSearchQuery.getEndPrice() != null && mallSearchQuery.getEndPrice() > 0) {
			criteria.andMallPerCapitaBetween(Integer.parseInt(mallSearchQuery.getBeginPrice() + ""),
					Integer.parseInt(mallSearchQuery.getEndPrice() + ""));
		}

		if (mallSearchQuery.getNearDistance() != null && mallSearchQuery.getNearDistance() > 0
				&& mallSearchQuery.getGetGaodeLongitude() != null && mallSearchQuery.getGetGaodeLongitude() > 0
				&& mallSearchQuery.getGaodeLatitude() != null && mallSearchQuery.getGaodeLatitude() > 0
				&& mallSearchQuery.getRectangles() != null && mallSearchQuery.getRectangles().length == 4) {
			criteria.andGaodeLatitudeBetween(mallSearchQuery.getRectangles()[2], mallSearchQuery.getRectangles()[3]);
			criteria.andGaodeLongitudeBetween(mallSearchQuery.getRectangles()[0], mallSearchQuery.getRectangles()[1]);
		}

		if (mallSearchQuery.getId() != null && mallSearchQuery.getId() > 0) {
			criteria.andIdEqualTo(mallSearchQuery.getId());
		}

		if (mallSearchQuery.getIds() != null && mallSearchQuery.getIds().length > 0) {
			criteria.andIdIn(Arrays.asList(mallSearchQuery.getIds()));
		}

		if (mallSearchQuery.getMallMainCategoryId() != null && mallSearchQuery.getMallMainCategoryId() > 0) {
			criteria.andMallCategoryIdEqualTo(mallSearchQuery.getMallMainCategoryId());
		}

		if (mallSearchQuery.getMallSecCategoryId() != null && mallSearchQuery.getMallSecCategoryId() > 0) {
			criteria.andMallSecCategoryIdEqualTo(mallSearchQuery.getMallSecCategoryId());
		}

		if (StringUtils.isNotBlank(mallSearchQuery.getKeyWord())) {
			criteria.andPyNameLike("%" + mallSearchQuery.getKeyWord() + "%");
		}
		
		if (mallSearchQuery.getSsig() != null) {
			criteria.andSsigEqualTo(mallSearchQuery.getSsig());
		}
		
		if (mallSearchQuery.getSearchPingtaiPingFen() != null) {
			if (mallSearchQuery.getSearchPingtaiPingFen() == 1) {
				criteria.andPingtaiPingFenEqualTo(5);
			}
			if (mallSearchQuery.getSearchPingtaiPingFen() == 2) {
				criteria.andPingtaiPingFenEqualTo(4);
			}
			if (mallSearchQuery.getSearchPingtaiPingFen() == 3) {
				criteria.andPingtaiPingFenEqualTo(3);
			}
		}
		
		if (mallSearchQuery.getSearchPrice() != null) {
			if (mallSearchQuery.getSearchPrice() == 1) {
				criteria.andMallPerCapitaBetween(0, 50);
			} else if (mallSearchQuery.getSearchPrice() == 2) {
				criteria.andMallPerCapitaBetween(50, 100);
			} else if (mallSearchQuery.getSearchPrice() == 3) {
				criteria.andMallPerCapitaBetween(100, 100000);
			}
		}

		return mallReadMapper.selectByExample(example);
	}

	@Override
	public int insertMall(Mall mall) {
		// TODO Auto-generated method stub
		return mallWriteMapper.insertSelective(mall);
	}

	@Override
	public Mall getMallByUserIdAndOnlyKey(Long userId, Integer onlyKey) {
		if (userId == null || onlyKey == null) {
			return null;
		}
		MallExample example = new MallExample();
		Criteria criteria = example.createCriteria();
		criteria.andSellerIdEqualTo(userId);
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		List<Mall> mallList = this.mallReadMapper.selectByExample(example);
		Mall mall = null;
		if(mallList != null && mallList.size()>0){
			 mall = mallList.get(0);
		}
		return mall;
	}

	@Override
	public Mall getMallByOnlyKey(Integer onlyKey) {
		MallExample example = new MallExample();
		Criteria criteria = example.createCriteria();
		criteria.andCommOnlyKeyEqualTo(onlyKey);
		List<Mall> mallList = this.mallReadMapper.selectByExample(example);
		Mall mall = null;
		if(mallList != null && mallList.size()>0){
			 mall = mallList.get(0);
		}
		return mall;
	}

	@Override
	public Integer modifyMallByPrimaryKey(Mall mall) {
		mall.setGmtModify(new Date());
		return this.mallWriteMapper.updateByPrimaryKey(mall);
		
	}

}
